<template>
  <div class="explore">
    <!-- 侧边菜单 -->
    <aside class="sidebar">
      <div class="sidebar-content">
        <h3 class="sidebar-title">车型分类</h3>
        <div class="menu-container">
          <div class="menu-item" v-for="category in carCategories" :key="category.id">
            <div class="main-menu" @click="toggleSubMenu(category.id)">
              <span>{{ category.name }}</span>
              <i class="arrow-icon">{{ category.isOpen ? '▼' : '►' }}</i>
            </div>
            <div class="sub-menu" v-if="category.isOpen">
              <div class="sub-menu-item" v-for="brand in category.brands" :key="brand.id"
                @click="goToShop(category.name, brand.name)">
                {{ brand.name }}
              </div>
            </div>
          </div>
        </div>
      </div>
    </aside>

    <div class="main-content">
      <div class="carousel-container">
        <div class="carousel" @mouseenter="pauseAutoPlay" @mouseleave="resumeAutoPlay">
          <div class="carousel-item" v-for="(car, index) in featuredCars" :key="car.id"
            :class="{ active: currentIndex === index }">
            <img :src="car.image" :alt="car.name">
            <div class="car-info">
              <h3>{{ car.name }}</h3>
              <p>{{ car.price }}</p>
              <button @click="goToConfig">立即配置</button>
            </div>
          </div>
          <button class="carousel-control prev" @click="prevSlide">‹</button>
          <button class="carousel-control next" @click="nextSlide">›</button>
        </div>
      </div>

      <div class="configuration" v-if="selectedCar">
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed, onMounted, onBeforeUnmount } from 'vue';
import { useRouter } from 'vue-router';

const router = useRouter();

const carCategories = ref([
  { id: 1, name: '轿车', isOpen: false, brands: [{ id: 101, name: '奔驰' }, { id: 102, name: '宝马' }, { id: 103, name: '奥迪' }] },
  { id: 2, name: 'SUV', isOpen: false, brands: [{ id: 201, name: '奔驰GLC' }, { id: 202, name: '宝马X5' }, { id: 203, name: '奥迪Q5' }] },
  { id: 3, name: '新能源', isOpen: false, brands: [{ id: 301, name: '奔驰EQE' }, { id: 302, name: '宝马iX3' }, { id: 303, name: '奥迪e-tron' }] }
]);

const featuredCars = ref([
  { id: 1, name: '奔驰 E级', price: '¥439,900起', image: 'src/images/demo1.jpg' },
  { id: 2, name: '奔驰 S级', price: '¥899,900起', image: 'src/images/car1.jpg' },
  { id: 3, name: '奔驰 GLC', price: '¥399,900起', image: 'src/images/demo.jpg' },
  { id: 4, name: '奔驰 AMG', price: '¥799,900起', image: 'src/images/demo1.jpg' },
  { id: 5, name: '奔驰 CLE', price: '¥359,900起', image: 'src/images/demo.jpg' },
]);

const currentIndex = ref(0);
const autoPlayInterval = ref(null);
const selectedCar = ref(null);

const currentCar = computed(() => {
  return featuredCars.value[currentIndex.value];
});

const toggleSubMenu = (categoryId) => {
  carCategories.value.forEach(category => {
    category.isOpen = category.id === categoryId ? !category.isOpen : false;
  });
};

const goToShop = (category, brand) => {
  router.push({ name: 'Shop', query: { category, brand } });
};

const nextSlide = () => {
  currentIndex.value = (currentIndex.value + 1) % featuredCars.value.length;
};

const prevSlide = () => {
  currentIndex.value = (currentIndex.value - 1 + featuredCars.value.length) % featuredCars.value.length;
};

const startAutoPlay = () => {
  autoPlayInterval.value = setInterval(nextSlide, 5000);
};

const pauseAutoPlay = () => {
  clearInterval(autoPlayInterval.value);
};

const resumeAutoPlay = () => {
  startAutoPlay();
};

const goToConfig = () => {
  router.push({ name: 'config', params: { modelId: currentCar.value.id } });
};

onMounted(() => {
  startAutoPlay();
});

onBeforeUnmount(() => {
  clearInterval(autoPlayInterval.value);
});
</script>

<style scoped>
.explore {
  position: relative;
  min-height: 100vh;
}

.sidebar {
  position: absolute;
  left: 0;
  top: 0;
  height: 100%;
  width: 260px;
  background: rgba(0, 0, 0, 0.5);
  backdrop-filter: blur(10px);
  padding: 20px 0;
  box-sizing: border-box;
  z-index: 10;
}

.carousel-container {
  width: 100vw;
  height: 90vh;
  overflow: hidden;
  position: relative;
}

.main-content {
  flex: 1;
  overflow: hidden;
}

.sidebar-content {
  padding: 0 15px;
}

.sidebar-title {
  color: #fff;
  font-size: 18px;
  margin-bottom: 20px;
  padding-left: 15px;
}

.menu-container {
  color: #fff;
}

.main-menu {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 15px;
  cursor: pointer;
  transition: background-color 0.3s;
}

.main-menu:hover {
  background: rgba(255, 255, 255, 0.2);
}

.arrow-icon {
  font-size: 12px;
}

.sub-menu {
  padding-left: 25px;
  background: rgba(0, 0, 0, 0.1);
}

.sub-menu-item {
  padding: 10px 15px;
  cursor: pointer;
  transition: all 0.3s;
}

.sub-menu-item:hover {
  background: rgba(255, 255, 255, 0.2);
  padding-left: 20px;
}

.carousel {
  height: 100%;
  transition: transform 0.5s ease;
}

.carousel-item {
  width: 100%;
  height: 100%;
  position: relative;
  opacity: 0;
  transition: opacity 0.5s ease;
  display: none;
}

.carousel-item.active {
  opacity: 1;
  display: block;
}

.carousel-item img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.car-info {
  position: absolute;
  bottom: 2rem;
  right: 2rem;
  width: auto;
  background: rgba(0, 0, 0, 0.7);
  color: white;
  padding: 1.5rem;
  border-radius: 8px;
  text-align: right;
  max-width: 350px;
}

.car-info h2 {
  margin: 0 0 0.5rem 0;
  font-size: 1.8rem;
}

.car-info .price {
  margin: 0 0 1rem 0;
  font-size: 1.2rem;
  color: #f0f0f0;
}

.car-info .config-button {
  background: transparent;
  border: 2px solid white;
  color: white;
  padding: 1.2rem 2.5rem;
  border-radius: 20px;
  cursor: pointer;
  font-weight: bold;
  font-size: 1.2rem;
  transition: all 0.3s ease;
}

.car-info .config-button:hover {
  background: rgba(255, 255, 255, 0.2);
}

.carousel-control {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  background: rgba(255, 255, 255, 0.3);
  border: none;
  color: white;
  font-size: 2rem;
  width: 50px;
  height: 50px;
  border-radius: 50%;
  cursor: pointer;
  transition: all 0.3s ease;
}

.carousel-control:hover {
  background: rgba(255, 255, 255, 0.5);
}

.prev {
  left: 20px;
}

.next {
  right: 20px;
}
</style>
